package com.dreamchaser.familydevice.config;

import com.dreamchaser.familydevice.annotation.CurrentUser;
import com.google.common.base.Predicate;
import com.google.common.collect.Lists;
import io.swagger.annotations.ApiOperation;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import springfox.documentation.RequestHandler;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.List;

@Configuration
@EnableSwagger2
@ConditionalOnProperty(name = "swagger.enable", havingValue = "true")
@Profile({"dev", "test"})
public class Swagger2 {
    Predicate<RequestHandler> requestHandlerPredicate1 = RequestHandlerSelectors.basePackage("com.dreamchaser.familydevice.controller");
    Predicate<RequestHandler> requestHandlerPredicate2 = RequestHandlerSelectors.basePackage("com.dreamchaser.familydevice.controller");

    @Bean
    public Docket createRestApi() {
        //可以添加多个header或参数
        ParameterBuilder parameterBuilder = new ParameterBuilder();
        List<Parameter> parameters = Lists.newArrayList();
        parameterBuilder
                .parameterType("header")
                .name("Authorization")
                .description("header中Authorization字段用于认证")
                .modelRef(new ModelRef("string"))
                //非必需，这里是全局配置，然而在登陆的时候是不用验证的
                .required(false).build();
        parameters.add(parameterBuilder.build());
//        parameters.add(parameterBuilder2.build());
//        parameters.add(parameterBuilder3.build());
        return new Docket(DocumentationType.SWAGGER_2).groupName("财务管理").globalOperationParameters(parameters)
                .ignoredParameterTypes(CurrentUser.class)
                .apiInfo(apiInfo())
                .select()
                //加了ApiOperation注解的类，才生成接口文档
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                //包下的类，才生成接口文档
                .apis(RequestHandlerSelectors.basePackage("com.dreamchaser.familydevice")) //这个路径需要根据你自己的项目层级来写
                // swagger版本2.7写法
                //.apis(Predicates.or(requestHandlerPredicate1, requestHandlerPredicate2)) //这个路径需要根据你自己的项目层级来写
                .apis(RequestHandlerSelectors.basePackage("com.dreamchaser.familydevice.controller"))
                .paths(PathSelectors.any())
                .build();
//            .securitySchemes(security());
    }

    @Bean
    public Docket createRestApi2() {
        ParameterBuilder parameterBuilder = new ParameterBuilder();
        List<Parameter> parameters = Lists.newArrayList();
        parameterBuilder
                .parameterType("header")
                .name("Authorization")
                .description("header中Authorization字段用于认证")
                .modelRef(new ModelRef("string"))
                //非必需，这里是全局配置，然而在登陆的时候是不用验证的
                .required(false).build();
        parameters.add(parameterBuilder.build());
//        Predicate<RequestHandler> requestHandlerPredicate1 = RequestHandlerSelectors.basePackage("com.example.controller");
//        Predicate<RequestHandler> requestHandlerPredicate2 = RequestHandlerSelectors.basePackage("com.example.controller");
        return new Docket(DocumentationType.SWAGGER_2).groupName("资金理财").globalOperationParameters(parameters)
                .ignoredParameterTypes(CurrentUser.class)
                .apiInfo(apiInfo())
                .select()
                //加了ApiOperation注解的类，才生成接口文档
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                //包下的类，才生成接口文档
                .apis(RequestHandlerSelectors.basePackage("com.dreamchaser.familydevice.controller.ManagingMoney")) //这个路径需要根据你自己的项目层级来写
                // swagger版本2.7写法/.apis(Predicates.or(requestHandlerPredicate1,requestHandlerPredicate2)) //这个路径需要根据你自己的项目层级来写
                //.apis(RequestHandlerSelectors.basePackage("com.example.controller"))
                .paths(PathSelectors.any())
                .build();
//            .securitySchemes(security());
    }

    private ApiInfo apiInfo() {
        //……/swagger-ui.html
        return new ApiInfoBuilder()
                .title("家庭数据管理系统开发文档")
                .description("家庭数据管理系统：http://note.helper.cn")
                .termsOfServiceUrl("http://note.helper.cn")
                .version("1.0")
                .build();
    }

}

